window.phono = jQuery.phono({
	apiKey: "7dc3cacb3694d75bea38adbab0df388e",
  	onReady: function() {
    	setPhonoSessionId(this.sessionId);
  	},
  	audio: {
        type: "flash",
        direct: false,
        onPermissionBoxShow: function(event) {},
        onPermissionBoxHide: function(event) {}
	},	  
	phone: {
		ringTone: "/audio/Diggztone_DigitalSanity.mp3",
		ringbackTone: "/audio/ringback-uk.mp3",
	    headset: true,
	    onIncomingCall: function(event) {
	    	window.phonoCall = event.call;
	    	incomingCall();
            //onHangup: function() {}
            window.phonoCall.bind({
        	    onHangup: function(event){
    		        if(jQuery('#incommingcallwarning').length > 0){
        	        	if(jQuery('#incommingcallwarning').is(':visible')){
    		        		jQuery('#incommingcallwarning').remove(); 
    		        		jQuery('#overlay').fadeOut('fast');
    		        	}
	        	    }
    		    },
    
        		onError: function(event){
        		    console.log("Phone error2: " + event.reason);
        		}
    		});
	    },
	    onError: function(event) {
	        console.log("Phone error: " + event.reason);
	    }
	}
});

function makeCall(fromname,fromUserId,toUserId,event){
	jQuery.post("actionService.php",{"method": "getUser","userId":toUserId}, function(data){
		window.phonoCall = phono.phone.dial(data.phono_session, {

			gain: 25,
	        volume: 75,
	        mute: false,
	        pushToTalk: false,
            event: event,
            toId: toUserId,
            username: fromname,
			
	        // Events
	        onRing: function() {
	         	console.log("Ringing");
                FreiChat.sendExternalMessage(this.config.event,this.config.toId,this.config.username,"Calling!!");
	         	FreiChat.changeToRingingButton(getHeader("x-touserid"));
	        },
	        onAnswer: function() {
	          	console.log("Answered");
	          	FreiChat.changeToHangUpCallButton(getHeader("x-touserid"));
	        },
	        onHangup: function() {	        	
	      		FreiChat.changeToMakeCallCallButton(getHeader("x-touserid"));
	        },
	        onError: function() {
	          	console.log("Call Error");
	        },
	        headers: [{
                name: "x-fromname",
                value: fromname
	        },
	        {
                name: "x-fromuserid",
                value: fromUserId
            },
	        {
                name: "x-touserid",
                value: toUserId
            }]
	    });
	},"json");
}
function incomingCall(){	
	var message = "Anonymous call";
	var fromname = getHeader('x-fromname');
	if(fromname)
		message = "<b>"+fromname+"</b> is calling";
	id = getHeader("x-fromuserid");
	showYesNo(message, "",'incommingcallwarning', 'FreiChat.changeToHangUpCallButton('+id+');answer();', 'hangup()','Answer','Reject');
}
function getHeader(name){
	var headerSize = window.phonoCall.headers.length;
	for(var i = 0; i < headerSize; i++){
		var header = window.phonoCall.headers[i];
		if(header.name == name){
			return header.value;
			break;
		}
	}
	return undefined;
}
function answer(){
	window.phonoCall.answer();;
}
function hangup(id){
	window.phonoCall.hangup();
	window.phonoCall.stopAudio();
	FreiChat.changeToMakeCallCallButton(id);
}
function mute(mute){
	window.phonoCall.mute(mute);
}
function talking(talk){
	window.phonoCall.talking(talk);
}